package com.workcell.workdata.xthings.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.workcell.workdata.xthings.constant.HardwareStateEnum;
import com.workcell.workdata.xthings.dto.BaseFlowmeterVo;
import com.workcell.workdata.xthings.entity.BaseFlowmeter;
import com.workcell.workdata.xthings.mapper.BaseFlowmeterMapper;
import com.workcell.workdata.xthings.service.BaseFlowmeterService;
import org.springframework.stereotype.Service;

/**
 * 流量计型号
 *
 * @author 廖博文
 * @date 2022-11-28 10:15:50
 */
@Service
public class BaseFlowmeterServiceImpl extends ServiceImpl<BaseFlowmeterMapper, BaseFlowmeter> implements BaseFlowmeterService {

	/**
	 * 模糊查询分页
	 * @param baseFlowmeterVo
	 * @return
	 */
	@Override
	public Page<BaseFlowmeter> getBaseFlowmeterVaguePage(BaseFlowmeterVo baseFlowmeterVo) {
		LambdaQueryWrapper<BaseFlowmeter> baseFlowmeterCondition = new QueryWrapper<BaseFlowmeter>().lambda()
				.and(ObjectUtil.isNotEmpty(baseFlowmeterVo.getKeyWord()), i -> i
						.like(BaseFlowmeter::getFmName,baseFlowmeterVo.getKeyWord())
						.or()
						.like(BaseFlowmeter::getManufacturer,baseFlowmeterVo.getKeyWord()))
				.eq(ObjectUtil.isNotEmpty(baseFlowmeterVo.getState()),BaseFlowmeter::getState,baseFlowmeterVo.getState())
				.and(ObjectUtil.isNotEmpty(baseFlowmeterVo.getStartTime()) || ObjectUtil.isNotEmpty(baseFlowmeterVo.getEndTime())
						,i -> i.between(BaseFlowmeter::getCreateTime,baseFlowmeterVo.getStartTime(),baseFlowmeterVo.getEndTime()));
		Page<BaseFlowmeter> page = new Page<>(baseFlowmeterVo.getPageNum(), baseFlowmeterVo.getPageSize());
		page.addOrder(OrderItem.desc("create_time"));
		return this.page(page,baseFlowmeterCondition);
	}

	/**
	 * 修改流量计型号信息：此方法不能修改状态
	 * @param baseFlowmeter
	 * @return
	 */
	@Override
	public Boolean updateBaseFlowmeter(BaseFlowmeter baseFlowmeter) {
		Assert.isTrue(ObjectUtil.isNotEmpty(baseFlowmeter.getFmId()),"ID不能为空");
		baseFlowmeter.setState(null);
		// 查询该流量计的状态
		BaseFlowmeter baseFlowmeterOld = this.getById(baseFlowmeter.getFmId());
		Assert.isTrue(ObjectUtil.isNotEmpty(baseFlowmeterOld),"无该条记录，请核实您的操作");
		Assert.isTrue(HardwareStateEnum.DRAF.getCode().equals(baseFlowmeterOld.getState()),"状态为草稿才能修改");
		return this.updateById(baseFlowmeter);
	}

	/**
	 * 修改流量计型号信息状态
	 * @param baseFlowmeter
	 * @return
	 */
	@Override
	public Boolean updateStateById(BaseFlowmeter baseFlowmeter) {
		Assert.isTrue(ObjectUtil.isNotEmpty(baseFlowmeter.getFmId()),"ID不能为空");
		Assert.isTrue(ObjectUtil.isNotEmpty(baseFlowmeter.getState()),"状态不能为空");
		// 查询该流量计的状态
		BaseFlowmeter baseFlowmeterOld = this.getById(baseFlowmeter.getFmId());
		Assert.isTrue(ObjectUtil.isNotEmpty(baseFlowmeterOld),"无该条记录，请核实您的操作");
		if (HardwareStateEnum.INUSE.getCode().equals(baseFlowmeterOld.getState())
				|| HardwareStateEnum.SUSPEND.getCode().equals(baseFlowmeterOld.getState())){
			if (HardwareStateEnum.DRAF.getCode().equals(baseFlowmeter.getState())){
				Assert.isTrue(false,"当状态为使用中或暂停中时，不能修改为草稿状态");
			}
		}
		// 重新创建对象，以防前端误传其他参数
		BaseFlowmeter baseFlowmeterParam = new BaseFlowmeter();
		baseFlowmeterParam.setFmId(baseFlowmeter.getFmId());
		baseFlowmeterParam.setState(baseFlowmeter.getState());
		return this.updateById(baseFlowmeterParam);
	}

	/**
	 * 删除流量计型号信息
	 * @param fmId
	 * @return
	 */
	@Override
	public Boolean delBaseFlowmeterById(String fmId) {
		Assert.isTrue(ObjectUtil.isNotEmpty(fmId),"ID不能为空");
		// 查询该流量计的状态
		BaseFlowmeter baseFlowmeterOld = this.getById(fmId);
		Assert.isTrue(ObjectUtil.isNotEmpty(baseFlowmeterOld),"无该条记录，请核实您的操作");
		if (HardwareStateEnum.INUSE.getCode().equals(baseFlowmeterOld.getState())
				|| HardwareStateEnum.SUSPEND.getCode().equals(baseFlowmeterOld.getState())){
			Assert.isTrue(false,"当状态为使用中或暂停中时，不能删除该条记录");
		}
		// 删除记录
		return this.removeById(fmId);
	}
}
